// für CVS optimiert

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class haupt extends JPanel implements Runnable, KeyListener,
		MouseListener, MouseMotionListener {

	/**
	 * @param args
	 */
	private static final long serialVersionUID = 1L;
	boolean start = true;
	// public static int x;
	// public static int y;
	// public int posButtonID = 0;
	// public boolean clicked = false;
	public startmenu smenu;
	public boolean clicked = false;
	drawClass dc;
	// public hilfe help;
	public hilfe hlp;
	public level lvl;
	public boolean running = false;
	boolean gamover = false;
	public int clickID = 0;
	int lastkey = 0;

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		// x=600;
		// y=800;
		haupt hauptpanel = new haupt();
	}

	public haupt() {
		JFrame frame = new JFrame("Helikopter v0.4");
		frame.setSize(800, 600);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.add(this);
		frame.addKeyListener(this);
		frame.addMouseListener(this);
		frame.addMouseMotionListener(this);
		frame.setVisible(true);
		frame.setResizable(false);
		initialisiere();
	}

	private void initialisiere() {
		smenu = new startmenu();
		hlp = new hilfe();
		lvl = new level();
		dc = new drawClass(this, lvl.level);
		Thread t = new Thread(this);
		t.start();
	}

	public void run() {
		while (start) {
			if (smenu.clicked) {
				if (smenu.posButtonID == 4) {
					System.exit(0);
				} else if (smenu.posButtonID == 1) {
					this.running = true;
				}

			}

			if (running) {
				/*
				 * if(lastkey==KeyEvent.VK_RIGHT || lastkey==KeyEvent.VK_LEFT){
				 * dc.h.i=0; }else
				 */if (dc.h.down && dc.h.i < 20) {
					++dc.h.i;
				} else if (dc.h.i > -20) {
					--dc.h.i;
				}
				if (dc.h.y >= this.getHeight() && dc.h.down) {
					dc.h.i = 0;
					dc.h.y = this.getHeight() - 50;
				} else if ((dc.h.y <= 0 && !dc.h.down)) {
					dc.h.i = 0;
					dc.h.y = 0;
				}
				dc.h.y = dc.h.y + dc.h.i;

			}
			// if(lastkey!=0){
			move(lastkey);
			// }
			try {
				Thread.sleep(50);
			} catch (Exception e) {
			}
			// System.err.println(smenu.posButtonID+" "+smenu.clicked);
			// System.err.println(lastkey+"");
			repaint();
		}
	}

	public void paintComponent(Graphics g) {
		super.paintComponent(g);
		if (!running) {

			if (smenu.item == 1) {

			}
			if (smenu.item == 2) {
				lvl.drawObjects(g);
				smenu.item = lvl.item;
			}
			if (smenu.item == 3) {

				hlp.drawObjects(g);
				smenu.item = hlp.item;
			}
			if (smenu.item == 4) {

				System.exit(0);
			}
			if (smenu.item == 0) {

				smenu.drawObjects(g);
			}

		} else {

			dc.level = lvl.level;
			dc.drawObject(g);

		}
	}

	public void keyPressed(KeyEvent e) {
		// System.err.print(e.getKeyCode());
		int key = e.getKeyCode();
		if (this.dc.h != null) {
			/*
			 * if(key==KeyEvent.VK_DOWN){ dc.h.y=dc.h.y+10; }else
			 * if(key==KeyEvent.VK_RIGHT){ dc.h.x=dc.h.x+10; }else
			 * if(key==KeyEvent.VK_UP){ dc.h.y=dc.h.y-10; }else
			 * if(key==KeyEvent.VK_LEFT){ dc.h.x=dc.h.x-10; }
			 */

			/*
			 * if(key==KeyEvent.VK_UP){ dc.h.down=false;
			 * 
			 * }else if(key==KeyEvent.VK_RIGHT && dc.h.x+50<=this.getWidth()){
			 * dc.h.x=dc.h.x+10; dc.h.down=false; //dc.h.i=0; }else
			 * if(key==KeyEvent.VK_LEFT && dc.h.x>=0){ dc.h.x=dc.h.x-10;
			 * dc.h.down=false; //dc.h.i=0; }
			 */

			lastkey = key;

		}

		if (key == KeyEvent.VK_ENTER) {
			this.running = false;
			smenu.clicked = false;
		}
	}

	public void keyReleased(KeyEvent e) {
		lastkey = 0;
		dc.h.down = false;
	}

	public void keyTyped(KeyEvent e) {

	}

	public void mouseMoved(MouseEvent e) {
		smenu.clicked = false;
		hlp.clicked = false;
		lvl.clicked = false;
		smenu.x = e.getX();
		smenu.y = e.getY();
		hlp.x = e.getX();
		hlp.y = e.getY();
		lvl.x = e.getX();
		lvl.y = e.getY();

		// repaint();
	}

	public void mouseClicked(MouseEvent e) {
		smenu.clicked = true;
		smenu.x = e.getX();
		smenu.y = e.getY();
		// repaint();
	}

	public void mouseReleased(MouseEvent e) {
	}

	public void mouseDragged(MouseEvent e) {
	}

	public void mouseEntered(MouseEvent e) {
	}

	public void mouseExited(MouseEvent e) {
	}

	public void mousePressed(MouseEvent e) {
		smenu.clicked = true;
		hlp.clicked = true;
		lvl.clicked = true;
		smenu.x = e.getX();
		smenu.y = e.getY();
		hlp.x = e.getX();
		hlp.y = e.getY();
		lvl.x = e.getX();
		lvl.y = e.getY();

		// repaint();
	}

	public void move(int key) {
		if (key == 0) {
			dc.h.down = true;
		} else {
			if (key == KeyEvent.VK_UP) {
				dc.h.down = false;

			} else if (key == KeyEvent.VK_RIGHT
					&& dc.h.x + 50 <= this.getWidth() + 10) {
				dc.h.x = dc.h.x + 10;
				dc.h.down = false;
				// dc.h.i=0;
			} else if (key == KeyEvent.VK_LEFT && dc.h.x >= 0) {
				dc.h.x = dc.h.x - 10;
				dc.h.down = false;
				// dc.h.i=0;
			}
		}
	}

}